package com.xbao.service.impl;

import com.aliyun.oss.OSSClient;
import com.xbao.enums.EnumUtil;
import com.xbao.enums.FileTypeEnum;
import com.xbao.enums.OssTargetTypeEnum;
import com.xbao.service.OssService;
import org.apache.commons.io.FileUtils;
import org.apache.lucene.codecs.CodecUtil;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.UUID;

/**
 * Created by qingshengzheng on 2019/1/22.
 */
@Service
public class OssServiceImpl implements OssService {
    @Resource
    private OSSClient ossClient;
    @Value("${oss.bucketName}")
    public  String bucketName ;

    @Override
    public void uploadByInputStream(Integer targetType,Integer fileType, MultipartFile multifile) {
        String fname=multifile.getOriginalFilename();
        File objfile = new File(fname);
        try {
            FileUtils.copyInputStreamToFile(multifile.getInputStream(), objfile);
        } catch (IOException e) {
            e.printStackTrace();
        }
        String filename = UUID.randomUUID().toString().replaceAll("-", "");
        String suffix= EnumUtil.getByCode(FileTypeEnum.class,fileType+"").getSuffix();
        String filedir=EnumUtil.getByCode(OssTargetTypeEnum.class,targetType+"").getLocation();;
        String object=filedir+filename+"."+suffix;
        ossClient.putObject(bucketName, filename, objfile);

    }
}
